<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" >
				<el-form-item>
					<el-input v-model="keyword" placeholder="请输入关键字..."></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="keywordQuery">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
    <!-- :data :代表可变的，没有写死 -->
		<el-table :data="dictionaryTypes" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;" @row-dblclick="queryDetail">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="name" label="名称" width="120">
			</el-table-column>
			<el-table-column prop="sn" label="编号" width="150">
			</el-table-column>
			<el-table-column label="操作">
				<template scope="scope">
					<el-button size="small" @click="handleSave(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :current-page="currentPage"
                     :page-size="pageSize" :total="totals" style="float:right;">
      </el-pagination>
		</el-col>

		<!--编辑和添加的对话框-->
		<el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">
			<el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
        <!-- prop="name" 需要验证就加上这个属性，不需要验证就不加 -->
        <el-form-item label="名称" prop="name">
          <el-input v-model="saveForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="编号" prop="sn">
          <el-input v-model="saveForm.sn" auto-complete="off"></el-input>
        </el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="saveFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
			</div>
		</el-dialog>
    <!--================================  start  ====================================-->

    <!--数据字典明细对话框-->
    <el-dialog  title="数据字典明细" :visible.sync="detailFormVisible" :close-on-click-modal="false">
      <!-- 高级查询和新增   -->

      <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
        <el-form :inline="true" >
          <el-form-item>
            <el-input v-model="keyword_d" placeholder="请输入关键字..."></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="keywordQuery_d">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleAdd_d">新增</el-button>
          </el-form-item>
        </el-form>
      </el-col>

      <!-- 明细列表数据-->
      <el-table :data="dictionaryDetails" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column type="index" width="60">
        </el-table-column>
        <el-table-column prop="name" label="名称" width="120">
        </el-table-column>
        <el-table-column prop="types_id" label="编号" width="150">
        </el-table-column>

        <el-table-column label="操作">
          <template scope="scope">
            <el-button size="small" @click="handleSave_d(scope.$index, scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="handleDel_d(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 批量删除  -->
      <el-col :span="24" class="toolbar">
        <el-button type="danger" @click="batchRemove_d" :disabled="this.sels.length===0">批量删除</el-button>
        <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange_d" :current-page="currentPage"
                       :page-size="pageSize" :total="totals" style="float:right;">
        </el-pagination>
      </el-col>

    </el-dialog>

    <!-- Detail明细 的添加和修改的对话框-->
    <el-dialog :title="title" :visible.sync="detailSaveFormVisible" :close-on-click-modal="false">
      <el-form :model="detailSaveForm" label-width="80px" :rules="detailSaveFormRules" >
        <!-- prop="name" 需要验证就加上这个属性，不需要验证就不加 -->
        <el-form-item label="名称" prop="name">
          <el-input v-model="detailSaveForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="编号" prop="types_id">
          <el-input v-model="detailSaveForm.types_id" auto-complete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="detailSaveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit_d" :loading="saveLoading">提交</el-button>
      </div>
    </el-dialog>
    <!-- =============================== end ==================================-->
	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getDictionarytypeListPage, removedictionarytype, batchRemovedictionarytype, savedictionarytype, adddictionarytype } from '../../api/api';

	export default {
		data() {
			return {
			  types_id: '',
			  keyword: '',
        keyword_d: '',
			  //模型数据
				dictionaryTypes: [],
        dictionaryDetails: [],
				totals: 0,
				currentPage: 1,
        pageSize:5,
				listLoading: false,//加载框

				sels: [],//列表选中列

        //1.添加和修改的相关数据
        title: '',

				saveFormVisible: false,//控制对话框是否显示 - 默认不显示
				saveLoading: false,    //加载框
				saveFormRules: {
					name: [
						{ required: true, message: '请输入名称', trigger: 'blur' }
					],
          sn: [
            { required: true, message: '请输入编号', trigger: 'blur' }
          ]
				},
        detailSaveFormVisible: false,//控制对话框是否显示 - 默认不显示
        detailSaveLoading: false,    //加载框
      /*  detailSaveFormRules: {
          name: [
            { required: true, message: '请输入名称', trigger: 'blur' }
          ],
          types_id: [
            { required: true, message: '请输入编号', trigger: 'blur' }
          ]
        },*/


				//2.编辑界面数据
				saveForm: {
					id: null,
					name: '',
					sn: '',
				},
        //2.1. 明细编辑界面数据
        detailSaveForm: {
          id: null,
          name: '',
          types_id: '',
        },

				addFormVisible: false,//类型新增界面是否显示
				addLoading: false,
				addFormRules: {
					name: [
						{ required: true, message: '请输入名称', trigger: 'blur' }
					],
          sn: [
            { required: true, message: '请输入编号', trigger: 'blur' }
          ]
				},

        detailAddFormVisible: false,//明细新增界面是否显示
        detailAddLoading: false,
        detailAddFormRules: {
          name: [
            { required: true, message: '请输入名称', trigger: 'blur' }
          ],
          types_id: [
            { required: true, message: '请输入编号', trigger: 'blur' }
          ]
        },

				//新增界面数据
				addForm: {
          id: null,
          name: '',
          sn: '',
				},
        detailAddForm: {
          id: null,
          name: '',
          types_id: '',
        },
        detailFormVisible: false,//明细列表界面是否显示
        detailLoading: false,
        detailFormRules: {
          name: [
            { required: true, message: '请输入名称', trigger: 'blur' }
          ],
          types_id: [
            { required: true, message: '请输入编号', trigger: 'blur' }
          ]
        },

			}
		},
		methods: {
		  //0.0双击弹出对话框 查看明细
		  queryDetail: function(row){
		    this.types_id = row.id;
		    this.getDictionaryDetails();
		    this.detailFormVisible = true;
      },

      //1.批量删除按钮，选项改变的时候就会触发
    selsChange: function (sels) {
      this.sels = sels;
    },
    //2.批量删除
    batchRemove: function () {
      //获取数组中每一个对象的id，- 数组
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;//显示列表的加载框
        this.$http.patch("/dictionaryType/",ids).then(res =>{
          this.listLoading = false;
          if(res.data.success){
            this.$message("删除成功")
            this.getDictionaryTypes();
          }else{
            this.$message.error("网络异常，请稍后重试【500】");
          }
        }).catch((res) =>{
          this.$message.error("网络异常，请稍后重试【服务器没开，404,400】")
        })
      }).catch(()=>{
      })
    },
      batchRemove_d: function () {
        //获取数组中每一个对象的id，- 数组
        var ids = this.sels.map(item => item.id);
        this.$confirm('确认删除选中记录吗？', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;//显示列表的加载框
          this.$http.patch("/dictionaryDetail/",ids).then(res =>{
            this.listLoading = false;
            if(res.data.success){
              this.$message("删除成功")
              this.getDictionaryDetails();
            }else{
              this.$message.error("网络异常，请稍后重试【500】");
            }
          }).catch((res) =>{
            this.$message.error("网络异常，请稍后重试【服务器没开，404,400】")
          })
        }).catch(()=>{
        })
      },

      //3.删除
      handleDel: function (index, row) {
        this.$confirm("确认删除该记录吗？","提示",{
          type: 'warning'
        }).then(() =>{
          this.listLoading =true;
          this.$http.delete("/dictionaryType/"+row.id).then(res => {
                this.listLoading = false;
                if(res.data.success){
                  this.$message.success("删除成功");
                  this.getDictionaryTypes();

                  //删除优化:计算总页数
                  var totalPage=Math.ceil(this.totals/this.pageSize);
                  //删除最后一页的最后一条数据，而且不能是第一页的时候，让当前页=当前页-1
                  if(
                      this.currentPage==totalPage &&(this.totals-1)%this.pageSize==0&& this.currentPage!=1
                  ){
                    this.currentPage =this.currentPage-1;
                  }
                }else{
                  this.$message.error("网络异常，请稍后重试【500】")
                }
            }).catch( res=>{
              this.message.error("网络异常，请稍后重试[服务器没开，404,400]")
          })
        }).catch(() => {
        })
      },
      handleDel_d: function (index, row) {
        this.$confirm("确认删除该记录吗？","提示",{
          type: 'warning'
        }).then(() =>{
          this.listLoading =true;
          this.$http.delete("/dictionaryDetail/"+row.id).then(res => {
            this.listLoading = false;
            if(res.data.success){
              this.$message.success("删除成功");
              this.getDictionaryDetails();

              //删除优化:计算总页数
              var totalPage=Math.ceil(this.totals/this.pageSize);
              //删除最后一页的最后一条数据，而且不能是第一页的时候，让当前页=当前页-1
              if(
                  this.currentPage==totalPage &&(this.totals-1)%this.pageSize==0&& this.currentPage!=1
              ){
                this.currentPage =this.currentPage-1;
              }
            }else{
              this.$message.error("网络异常，请稍后重试【500】")
            }
          }).catch( res=>{
            this.message.error("网络异常，请稍后重试[服务器没开，404,400]")
          })
        }).catch(() => {
        })
      },
		  //4.高级查询
		  keywordQuery(){
        //防止高级查询时查询出为空的现象【当前是第5页，但是高级查询的结果没有5页】
        this.currentPage=1;
        this.getDictionaryTypes();
      },
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getDictionaryTypes();
			},
      keywordQuery_d(){
        //防止高级查询时查询出为空的现象【当前是第5页，但是高级查询的结果没有5页】
        this.currentPage=1;
        this.getDictionaryDetails();
      },
      handleCurrentChange_d(val) {
        this.currentPage = val;
        this.getDictionaryDetails();
      },
			//5.获取数据类型列表
			getDictionaryTypes() {
			  //组装参数
				let para = {
					currentPage: this.currentPage,
          pageSize: this.pageSize,
          //高级查询的参数
          keyword: this.keyword
				};
				//显示记载框
				this.listLoading = true;
				//NProgress.start();

        this.$http.post("/dictionaryType",para).then(res =>{
          this.totals = res.data.totals;
          this.dictionaryTypes = res.data.data;
          this.listLoading = false
        }).catch(res =>{
          this.$message.error("网络繁忙，请稍后重试!!!")
            })
			},
      //5.1获取数据明细列表
      getDictionaryDetails(){
        //组装参数
        let para = {
          currentPage: this.currentPage,
          pageSize: this.pageSize,
          //高级查询的参数
          keyword_d: this.keyword_d,
          types_id: this.types_id

        };
        //显示加载框
        this.listLoading = true;
        //NProgress.start();

        this.$http.post("/dictionaryDetail",para).then(res =>{
          this.totals = res.data.totals;
          this.dictionaryDetails = res.data.data;
          this.listLoading = false
        }).catch(res =>{
          this.$message.error("网络繁忙，请稍后重试!!!")
        })

      },
			//6.显示编辑界面的对话框
			handleSave: function (index, row) {
        this.title = "编辑";
				this.saveFormVisible = true;
				this.saveForm = Object.assign({}, row);
			},
      //6.1明细字典显示编辑界面的对话框
      handleSave_d: function (index, row) {
        this.title = "编辑";
        this.detailSaveFormVisible = true;
        this.detailSaveForm = Object.assign({}, row);
      },

			//7.显示新增界面的对话框
			handleAdd: function () {
        this.title = "添加";
				this.saveFormVisible = true;
				this.saveForm = {
          id: null,
          name: '',
          sn: '',
				};
			},
      //7.1字典明细的新增对话框
      handleAdd_d: function () {
        this.title = "添加";
        this.detailSaveFormVisible = true;
        this.detailSaveForm = {
          id: null,
          name: '',
          types_id: '',
        };
      },
			//8.点击提交数据
			saveSubmit: function () {
        //将所有ref的表单【ref="saveForm"】,validate-验证
				this.$refs.saveForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
              //显示提交的加载框
							this.saveLoading = true;

							//解决上级部门 下拉框选择出现的问题
							let para = Object.assign({}, this.saveForm);

              //发送请求
							this.$http.put("/dictionaryType",para).then((res) => {
                //关闭提交的加载框
								this.saveLoading = false;
                //关闭对话框
                this.saveFormVisible = false;

                if(res.data.success){
                  this.$message.success("操作成功");
                  this.getDictionaryTypes();
                }else{
                  this.$message.error("系统异常，请稍后重试!【500】");
                }
							}).catch((res)=>{
                this.$message.error("系统异常，请稍后重试!【服务器没开,404,400,405】")
              })
						});
					}
				});
			},
      //8.1点击提交 数据字典明细数据
      saveSubmit_d: function () {
		    //表单验证规则
        //将所有ref的表单【ref="saveForm"】,validate-验证
        /*this.$refs.detailSaveForm.validate((valid) => {
          if (valid) {*/
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              //显示提交的加载框
              this.saveLoading = true;

              //传递对话框 参数
              let para = {"id": this.detailSaveForm.id, "name": this.detailSaveForm.name,"types_id": this.types_id}

              //发送请求
              this.$http.put("/dictionaryDetail",para).then((res) => {
                //关闭提交的加载框
                this.saveLoading = false;
                //关闭对话框
                this.saveFormVisible = false;

                if(res.data.success){
                  this.$message.success("操作成功");
                  this.getDictionaryDetails();
                }else{
                  this.$message.error("系统异常，请稍后重试!【500】");
                }
              }).catch((res)=>{
                this.$message.error("系统异常，请稍后重试!【服务器没开,404,400,405】")
              })
            });
        /*  }
        });*/
      },
		},
		mounted() {
			this.getDictionaryTypes();
		}
	}

</script>

<style scoped>

</style>